package com.wephone.Student.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.wephone.Student.R;
import com.wephone.Student.bean.data.CountryData;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.task.GetCountryListAsyncTask;
import com.wephone.Student.util.ImageLoaderUtil;
import com.wephone.Student.util.ShareReferenceUtil;
import com.wephone.Student.util.Util;

public class BaseActivity extends Activity {
//	public static UserInfoData myInfo = null;
	protected static HttpClient httpClient = new HttpClient();
	protected static ImageLoader imageLoader;
	private static Activity currActivity;
	private static List<CountryData> countryList = new ArrayList<CountryData>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		imageLoader = ImageLoaderUtil.getInstance(getApplicationContext(), R.drawable.default_add_picture);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		currActivity = this;
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	public static void showNotifyDialog(){
		try{
			new AlertDialog.Builder(currActivity)
			.setTitle("提示")
			.setMessage("您收到了一条新消息!")
			.setPositiveButton("确定", null).show();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static UserInfoData infoData;
	
	public static UserInfoData getMyInfo(Context context){
		if(infoData==null){
			String userInfoJson = ShareReferenceUtil.getUserInfoJson(context);
			if(!TextUtils.isEmpty(userInfoJson)){
				Gson gson = new Gson();
				infoData = gson.fromJson(userInfoJson, UserInfoData.class);
				ShareReferenceUtil.saveUserId(context, infoData.getuId());
				String infoJson = gson.toJson(infoData);
				ShareReferenceUtil.saveUserInfoJson(context, infoJson);
			}
		}
		return infoData;
	}
	
	public static List<CountryData> getCountryList(Context context){
		if(countryList!=null && countryList.size()>0){
			return countryList;
		}else{
			List<CountryData> results = Util.getCountryList(context);
			countryList.addAll(results);
			return countryList;
		}
	}
	
	public static void initCountryList(Context context){
		new GetCountryListAsyncTask(context, handler).execute();
	}
	
	private static Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if(msg.what==11){
				List<CountryData> results = (List<CountryData>)msg.obj;
				countryList.clear();
				countryList.addAll(results);
			}
		}
	};
}
